//
//  AppDelegate.m
//  YuTang
//
//  Created by 葛悦彣 on 2024/7/1.
//

#import "AppDelegate.h"
#import "SCBaseTabBarViewController.h"
#import "SCLoginViewController.h"
#import "SCBaseNavigationViewController.h"
#import "JPUSHService.h"
#import <UserNotifications/UserNotifications.h>
#import "SJRotationManager.h"
@interface AppDelegate ()<JPUSHRegisterDelegate>

@end

@implementation AppDelegate


- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
    // Override point for customization after application launch.
    application.applicationIconBadgeNumber = 0;

  
    
    self.window = [[UIWindow alloc]initWithFrame:[UIScreen mainScreen].bounds];
    self.window.backgroundColor = [UIColor whiteColor];
    if([UserInfoModel shared].token.length > 0){
        self.window.rootViewController = [SCBaseTabBarViewController new];
    } else {
        SCBaseNavigationViewController * log = [[SCBaseNavigationViewController alloc] initWithRootViewController:[SCLoginViewController new]];
        self.window.rootViewController = log;
    }
    
    
    [self.window makeKeyAndVisible];
  
    
    
      
    //键盘
    IQKeyboardManager *manager = [IQKeyboardManager sharedManager];
    manager.enable = YES;
    manager.overrideKeyboardAppearance = YES;
    manager.shouldResignOnTouchOutside = YES;
    manager.enableAutoToolbar = YES;
    
    JPUSHRegisterEntity * entity = [[JPUSHRegisterEntity alloc] init];
    entity.types = JPAuthorizationOptionAlert|JPAuthorizationOptionBadge|JPAuthorizationOptionSound;
    [JPUSHService registerForRemoteNotificationConfig:entity delegate:self];
    [JPUSHService setupWithOption:launchOptions appKey: @"24531c7639d4673d738e6652"
                          channel:@"APPStore"
                 apsForProduction:1
            advertisingIdentifier:nil];
    [JPUSHService registrationIDCompletionHandler:^(int resCode, NSString *registrationID) {
        if(resCode == 0){
            NSLog(@"极光的ID%@",registrationID);
            NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
            [defaults setObject:registrationID forKey:@"APNSJGID"];
            [defaults synchronize];
        }
    }];
    return YES;
    
}



- (void)application:(UIApplication *)application didRegisterForRemoteNotificationsWithDeviceToken:(NSData *)deviceToken {
    NSMutableString *deviceTokenString1 = [NSMutableString string];
    const char *bytes = deviceToken.bytes;
    int iCount = deviceToken.length;
    for (int i = 0; i < iCount; i++) {
        [deviceTokenString1 appendFormat:@"%02x", bytes[i]&0x000000FF];
    }
    NSLog(@"APNStoken：%@", deviceTokenString1);
    [JPUSHService registerDeviceToken:deviceToken];

}





// iOS 10 Support
- (void)jpushNotificationCenter:(UNUserNotificationCenter *)center didReceiveNotificationResponse:(UNNotificationResponse *)response withCompletionHandler:(void (^)(void))completionHandler {
  // Required
  NSDictionary * userInfo = response.notification.request.content.userInfo;
  if([response.notification.request.trigger isKindOfClass:[UNPushNotificationTrigger class]]) {
    [JPUSHService handleRemoteNotification:userInfo];
  }
    NSLog(@"---%@---",userInfo);
        completionHandler();    // 系统要求执行这个方法
}
- (UIInterfaceOrientationMask)application:(UIApplication *)application supportedInterfaceOrientationsForWindow:(UIWindow *)window {
    return [SJRotationManager supportedInterfaceOrientationsForWindow:window];
}
@end
